package com.gzsxy.esjy.redis.extend.impl;

import cn.hutool.core.date.DateUtil;
import com.gzsxy.esjy.redis.extend.ICacheHashOneDay;
import com.gzsxy.esjy.redis.extend.RedisService;

import java.util.Date;
import java.util.Map;

/**
 * @author xiaolong
 * @version 1.0
 * @description: 一天缓存
 * @date 2021/10/19 15:47
 */
public class CacheHashOneDay implements ICacheHashOneDay {

    private final RedisService redisService;

    public CacheHashOneDay(RedisService redisService) {
        this.redisService = redisService;
    }

    @Override
    public void put(String key, String keyHash, Object value) {
        put(key, keyHash, value,1);

    }

    @Override
    public void put(String key, String keyHash, Object value, int day) {
        key = getDayKey(key);
        redisService.expire(key,day*24 * 60 * 60+1000);
        redisService.put(key,keyHash,value);
    }

    @Override
    public Object get(String key, String keyHash) {
        return redisService.get(getDayKey(key),keyHash);
    }

    @Override
    public <T> T get(String key, String keyHash, Class<T> clazz) {
        Object o = get(key, keyHash);
        return (T) o;
    }

    @Override
    public void remove(String key, String keyHash) {
        redisService.deleteHashKeys(getDayKey(key),keyHash);
    }

    @Override
    public Map<String, Object> getAll(String key) {
        return redisService.getMap(getDayKey(key));
    }

    private String getDayKey(String key){
        ////获取某天的开始 2022-08-23 00:00:00
        return DateUtil.beginOfDay(new Date()).getTime()+key;
    }

}
